package com.siae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.UploadFileMapper;
import com.siae.pojo.UploadFile;
import com.siae.pojo.vo.Result;
import com.siae.service.UploadFileService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UploadFileServiceImpl extends ServiceImpl<UploadFileMapper, UploadFile> implements UploadFileService {
    @Autowired
    private UploadFileMapper fileMapper;

    @Override
    public Result<List<UploadFile>> getFilePath(Integer id, String filePath) {
        try{
            QueryWrapper<UploadFile> wrapper = new QueryWrapper<>();
            wrapper
                    .eq(id != null && id != 0,"id",id)
                    .eq(StringUtils.isNotBlank(filePath),"file_path",filePath);
            List<UploadFile> uploadFiles = fileMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,uploadFiles);
        }catch (Exception e){
            log.error("获取文件列表失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<UploadFile> getUploadFileById(Integer id) {
        try {
            UploadFile uploadFile = fileMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,uploadFile);
        }catch (Exception e){
            log.error("查询用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<UploadFile> getUploadFileByName(String fileName) {
        try {
            QueryWrapper<UploadFile> wrapper = new QueryWrapper<>();
            wrapper.eq(StringUtils.isNotBlank(fileName),"file_name",fileName);
            UploadFile uploadFile = fileMapper.selectOne(wrapper);
            return Result.success(BizCode.SUCCESS,uploadFile);
        }catch (Exception e){
            log.error("查询用户角色失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result addUploadFile(UploadFile uploadFile) {
        try {
            uploadFile.setCreatedAt(new Date());
            uploadFile.setUpdatedAt(new Date());
            uploadFile.setIsDel(0);
            int i = fileMapper.insert(uploadFile);
            if (i > 0){
                return Result.success(BizCode.SUCCESS);
            }else {
                return Result.fail(BizCode.FAIL);
            }
        }catch (Exception e){
            log.error("添加文件失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result updateFile(UploadFile uploadFile) {
        try {
            int i = fileMapper.updateById(uploadFile);
            if (i > 0){
                return Result.success(BizCode.SUCCESS);
            }else {
                return Result.fail(BizCode.FAIL);
            }
        }catch (Exception e){
            log.error("修改文件失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result deleteFile(Integer id) {
        try {
            UploadFile uploadFile = fileMapper.selectById(id);
            uploadFile.setIsDel(1);
            int i = fileMapper.updateById(uploadFile);
            if (i > 0){
                return Result.success(BizCode.SUCCESS);
            }else {
                return Result.fail(BizCode.FAIL);
            }
        }catch (Exception e){
            log.error("删除文件失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

}
